package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author coolbook
 * @ClassName SetmealController
 * @Description TODO
 * @Date 10:26 2021/11/16
 * @Version 1.0
 */
@RestController
@RequestMapping("setmeal")
@CrossOrigin
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    /**
     * 查询所有套餐信息
     *
     * @return
     */
    @GetMapping("findAll")
    public Result findAll() {

        try {
            List<Setmeal> setmeals = setmealService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, setmeals);
        } catch (Exception e) {
            e.printStackTrace();
            //如果没有查询到 直接返回空集合
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /**
     * 查询套餐详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findbyId(@PathVariable("id") Integer id) {
        try {
            Setmeal setmeal = this.setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
